import { NotifyService } from '@abp/notify/notify.service';
import {
  Component,
  Injector,
  ViewChild,
  ViewEncapsulation
} from '@angular/core';
import { Http } from '@angular/http';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { MaterialChargeServiceProxy, WlReleaseStockBillEditDto,MaterialInportServiceProxy, ImportMaterialChargeInput } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { EditMaterialModalComponent } from './edit-material-modal.component';

@Component({
  templateUrl: './index.component.html',
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class MaterialIndexComponent extends AppComponentBase {
  @ViewChild('editMaterialModal') editMaterialModal: EditMaterialModalComponent;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  advancedFiltersAreShown = false;
  filterText = '';
  TaskCode = '';
  DrawingCode = '';
  MaterialName = '';
  MaterialStatus = '';
  InvFurBatch = '';
  Hgz_zjd = '';
  InvCode = '';
  PaiHao = '';
  MaterialSpec = '';
  Hgz_zyd = '';
  MaterialSize = '';
  InvMeasName = '';
  TechCondition = '';
  ZhuanYongInfo = '';
  HgzNo = '';

  constructor(
    injector: Injector,
    private _http: Http,
    private _materialServiceProxy: MaterialChargeServiceProxy,
    private _materialInportServiceProxy: MaterialInportServiceProxy,
    private _fileDownloadService: FileDownloadService
  ) {
    super(injector);
  }

  getMaterials(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._materialServiceProxy
      .getMaterialChargesZero(
        this.filterText,
        this.TaskCode,
        this.DrawingCode,
        this.MaterialName,
        this.MaterialStatus,
        this.InvFurBatch,
        this.Hgz_zjd,
        this.InvCode,
        this.PaiHao,
        this.MaterialSpec,
        this.Hgz_zyd,
        this.MaterialSize,
        this.InvMeasName,
        this.TechCondition,
        this.ZhuanYongInfo,
        this.HgzNo,
        this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        console.log(result);
      });
  }

  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }
  exportToExcel(event?: LazyLoadEvent): void {
    this._materialServiceProxy.getMaterialToExcel(
      this.filterText,
      this.TaskCode,
      this.DrawingCode,
      this.MaterialName,
      this.MaterialStatus,
      this.InvFurBatch,
      this.Hgz_zjd,
      this.InvCode,
      this.PaiHao,
      this.MaterialSpec,
      this.Hgz_zyd,
      this.MaterialSize,
      this.InvMeasName,
      this.TechCondition,
      this.ZhuanYongInfo,
      this.HgzNo,
      this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      100000
    ).subscribe(result => {
      this._fileDownloadService.downloadTempFile(result);
    });
  }
  onUpload(event) {

    let list = event as Array<any>;
   
    let materialList = list.map(el => {
      return {
        taskCode: el.任务号,
        drawingCode: el.图号,
        materialName: el.物资名称,
        invFurBatch: el.炉批号,
        hgz_zjd: el.化验号,
        invCode: el.物资编码,
        invXhPhCpdh: el.牌号,
        invStatus: el.物资状态,
        invScale: el.规格,
        jcCc: el.尺寸,
        techCondition: el.技术状态,
        hgzNo: el.合格证,
        remark: el.备注
      };
    }) as Array<ImportMaterialChargeInput>;

    this.message.confirm(
      '确认导入这' + list.length + '条数据吗？',
      this.l('AreYouSure'),
      (isConfirmed) => {
        if (isConfirmed) {
          this.primengTableHelper.showLoadingIndicator();
          this._materialInportServiceProxy.importMaterialCharge(materialList)
            .subscribe((result) => {
              this.primengTableHelper.hideLoadingIndicator();
              this.notify.success('导入数据成功');
            });
        }
      }
    );
  }
}
